
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponInfo: {}, //订单详情可用优惠券信息
    coupons:[],//优惠券
    selectCoupon: '',//已选优惠券信息{}
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onShow:function(){
    console.log('优惠券', app.data.couponInfo);
    this.setData({
      couponInfo: app.data.couponInfo
    })
    this.getCoupons();
  },
  /**
   * 获取优惠券
   */

  getCoupons:function(){
    let selectCoupon = app.data.selectCoupon;//已选优惠券信息
    let couponInfo = app.data.couponInfo;
    let coupons = couponInfo.zjWholePlaceCouponInfoVos;
    

    for (let i = 0; i < coupons.length;i++){
      let endDate = coupons[i].endDate;
      coupons[i].endDateStr = endDate.replace(/-/g, '.');
      if (selectCoupon != '' && selectCoupon.couponInfouserId == coupons[i].couponInfouserId){
        coupons[i].isSelected = true;
      }else{
        coupons[i].isSelected = false;
      }
    }
    couponInfo.zjWholePlaceCouponInfoVos = coupons;
    this.setData({
      couponInfo: couponInfo,
      coupons: coupons
    })
    console.log(coupons);
  },
  /**
   * 回到订单页
   */
  backToOrder:function(){
    app.clearSelectCoupon();
    setTimeout(()=>{
      wx.navigateBack({
        delta: 1,
      })
    },200)
  },
  /**
   * 选择优惠券
   */
  chooseCoupon:function(e){
    console.log('chooseCoupon',e);
    let index = e.currentTarget.dataset.index;
    let coupons = this.data.coupons;
    let selectCoupon = '';
    let couponInfouserId = '';
    for(let i = 0;i < coupons.length;i++){
      if(i == index){
        coupons[i].isSelected = true;
        couponInfouserId = coupons[i].couponInfouserId;
        selectCoupon = coupons[i];
      }else{
        coupons[i].isSelected = false;
      }
    }
    this.setData({
      coupons: coupons,
      couponInfouserId: couponInfouserId
    })
    app.saveSelectCoupon(selectCoupon);
    setTimeout(()=>{
      wx.navigateBack({
        delta: 1,
      })
    },500)
  
    console.log('coupons', coupons);
    
  }
})